import os
import re


def get_head_img_url():
    p1 = os.path.abspath(__file__)
    p2 = os.path.dirname(p1)
    p3 = os.path.dirname(p2)
    p4 = os.path.dirname(p3)
    return p4 + '/static/user/img/head/'


def get_file_son(path_: str) -> list:
    """
    返回指定路径内的所有文件 (不会返回路径内的文件夹)
    :param path_: 制定一个路径
    :return: list
    """
    path_list = []
    fl = os.listdir(path_)

    for f in fl:
        path_list.append(path_ + f + '/')

    return path_list


def get_file_all_son(path_: str):
    """
    返回指定路径内的所有文件 路径内文件夹内的文件也会返回 (不会返回路径内的文件夹)
    :param path_: 制定一个路径
    :return: list
    """
    path_list = []
    fl = None

    try:
        fl = os.listdir(path_)
    except NotADirectoryError or FileNotFoundError:
        return path_

    if not fl:
        return []

    path_list = get_file_son(path_)

    for pi in range(len(path_list)):
        path_list[pi] = get_file_all_son(path_list[pi])

    p2list = []

    for pi in path_list:
        if isinstance(pi, list):
            for pj in pi:
                p2list.append(pj)
        else:
            p2list.append(pi)

    return p2list


def get_default_head() -> list:
    """
    返回 static/user/img/head 文件夹内的图片(不会返回文件夹内的文件夹),图片后缀为jpg
    :return: list
    :example: ['7th.jpg', 'ayaka.jpg', 'barbara.jpg', 'ei.jpg', 'eula.jpg']
    """
    list_ = get_file_son(get_head_img_url())

    list__ = []

    for i in list_:
        list__.append(re.split('/', i)[-2])

    pop_index = []

    for i in range(len(list__)):
        if list__[i][-3:] != 'jpg':
            pop_index.append(i)

    for i in range(len(pop_index)):
        list__.pop(pop_index[i] - i)

    return list__


# default_head_list = get_default_head()

if __name__ == '__main__':
    default_head_list = get_default_head()
    print(default_head_list)
